package org.sunsys.qa.service.user;

import org.sunsys.qa.service.Service;
import org.sunsys.qa.service.response.ResponseStatus;
import org.sunsys.qa.service.response.UserValidationResponse;
import org.sunsys.qa.txnhandler.UserTxnsHandler;
import org.sunsys.qa.user.User;
import org.sunsys.qa.user.exceptions.UserValidationException;

public class UserValidationService implements Service<User, UserValidationResponse>{

	private UserTxnsHandler userTxnsHandler = null;
	
	public UserValidationService() {
		this.userTxnsHandler = new UserTxnsHandler();
	}
	
	public void setUserTxnsHandler(UserTxnsHandler userTxnsHandler) {
		this.userTxnsHandler = userTxnsHandler;
	}
	
	public UserTxnsHandler getUserTxnsHandler() {
		return userTxnsHandler;
	}
	
	@Override
	public UserValidationResponse service(User user) throws UserValidationException {

		/* */
		boolean doesUserExists = userTxnsHandler.doesUserExists(user);
		
		UserValidationResponse response = new UserValidationResponse();
		if( doesUserExists ) {
			response.setStatus(ResponseStatus.SUCCESS);
			response.setStatusMessage("User Id and credentials are valid");
		} else {
			response.setStatus(ResponseStatus.FAILED);
			response.setStatusMessage("Invalid User Id or credentials");
		}
		response.setUserId(user.getUserId());
		
		return response;
	}
}
